/**
 * 2018/10/31 星期三 10:48:32 Lynn Li created.
 */

package com.diver.flinkdemo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diver.flinkdemo.dto.xxt.TodaysSportDto;
import com.diver.flinkdemo.entity.xxt.DayExercise;
import com.diver.flinkdemo.mapper.mysqlMapper.DayExerciseMapper;
import com.diver.flinkdemo.service.IDayExerciseService;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 日运动量 Service 实现
 * Created by Lynn Li on 2018/10/31.
 */
//@Service
@Slf4j
public class DayExerciseServiceImpl implements IDayExerciseService {

//    @Autowired
    private DayExerciseMapper mapper;
    private SqlSession session;


    @Override
    public void doUpdateModelByStuId(DayExercise dayExercise) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession(true);
            mapper = session.getMapper(DayExerciseMapper.class);
            mapper.doUpdateModelByStuId(dayExercise);
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public DayExercise getStepByDate(String studentCardId, Long dateTime) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(DayExerciseMapper.class);
            log.debug("getStepByDate===>studentCardId:{}######",studentCardId);
            return mapper.getStepByDate(studentCardId, dateTime);
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public List<TodaysSportDto> getClassStepByDate(String studentCardId, Long dateTime) {

        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(DayExerciseMapper.class);
            DayExercise dayExercise = getStepByDate(studentCardId, dateTime);
            if (dayExercise != null) {
                if (StringUtils.isNotBlank(dayExercise.getClassName())){
                    return mapper.getClassStepByDate(dayExercise);
                }
                List<TodaysSportDto> todaysSportDtos = new ArrayList<>();
                TodaysSportDto todaysSportDto = new TodaysSportDto();
                try {
                    BeanUtils.copyProperties(dayExercise,todaysSportDto);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                todaysSportDtos.add(todaysSportDto);
                return todaysSportDtos;
                }
            return null;
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public List<TodaysSportDto> getTodaysSportAndMonth(String studentCardId) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(DayExerciseMapper.class);
            return mapper.getTodaysSportAndMonth(studentCardId);
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public List<DayExercise> sumDayExercise(Long newUploadDate) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(DayExerciseMapper.class);
            return mapper.sumDayExercise(newUploadDate);
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public List<DayExercise> listByParams(QueryWrapper<DayExercise> wrapper) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession(true);
            mapper = session.getMapper(DayExerciseMapper.class);
            return mapper.selectList(wrapper);
        } finally {
            endOfSession(session);
        }
    }

    public boolean save(DayExercise entity) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession(true);
            mapper = session.getMapper(DayExerciseMapper.class);
            return (mapper.insert(entity) == 1);
        } finally {
            endOfSession(session);
        }
    }

    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();
        }
    }
}
